/**
 * 
 */
define(function(require) {
	var comm = require('sdk/server');
	require('sdk/common');
	var userData = comm.getUser();
	require('box');
	var otherParam = appcan.ls.val('partsFilter');
	if (otherParam && otherParam.split) {
		otherParam = JSON.parse(otherParam)
	} else {
		otherParam = {}
	};
	
	//模板
	var dataTemp = $('#partListTemp').val();
	var dataRender = function(data, reload) {
		var render = etpl.compile(dataTemp);
		//console.log(data)
		var html = render(data);
		if(!$('#partsList').length){
			$('body').html('<div class="list partsList" id="partsList"></div>');
		};
		if (reload) {
			$('#partsList').html(html);
		} else {
			$('#partsList').append(html);
		};
		$('body').cacheImg().removeClass('h100').scrollLoad(getData);
		appcan.loading.hide();
	};

	var getData = function(reload) {
		if (reload) {
			$('body').addClass('h100');
			appcan.loading.show();
		}
		var finalParam = $.extend({
			method: "getSearchList",
			sid: appcfg.project.sid
		}, otherParam);
		appcan.toload({
			url: appcfg.host.control + '/goods/search.jsp',
			data: finalParam,
			size: 5,
			reload: reload,
			success: function(res) {
				appcan.window.resetBounceView(reload ? 0 : 1);
				if (res.status === 'Y') {
					dataRender(res, reload);
				} else {
					appcan.loading.hide();
					var emptyhtml = comm.commonTemp('parts');
					$('body').addClass('h100').html(emptyhtml);
				}
			}
		});
	};
	//列表事件
	$('body').on('click', '.item', function(e) {
		e.preventDefault();
		var $target = $(e.target);
		//添加到购物车
		if ($target.is('.addToCart')) {
			var id = $target.data('id'),
				title = $target.data('name'),
				need = $target.data('need'),
				stand = $target.data('stand'),
				remark = $target.data('remark'),
				min = $target.data('min');
			return appcan.openView({
				param: {
					id: id,
					title: title,
					remark: 1,
					minBuyCount: min
				}
			}, 'parts', 'detail');
		} else {
			//配件详细
			var id = $target.parents('.item').data('id'),
				title = $target.parents('.item').find('h3').text();
			if (id) {
				appcan.openView({
					param: {
						id: id,
						title: title
					}
				}, 'parts', 'detail');
			}
		}
	});
	//更新
	window.change = function() {
		$('body').addClass('h100');
		appcan.loading.show();
		otherParam = JSON.parse(appcan.ls.val('partsFilter'));
		getData(true);
	};

	appcan.ready(function() {
		appcan.window.setBounce({
			bounceType: ['0'],
			downEndCall: function(type) {
				getData(type==0);
			}
		});

		getData(true);


	});
});